<?php
	$ls_gridname	= 'dtl_sample2';


// ----------------- >> Form Detail << ----------------- ?>
<script type="text/javascript">
	

	$(document).ready(function(){
		// Setup Form
		$('#form_<?php echo $ls_gridname; ?>').setupForm([
			{ name: 'kode_penjualan_detail', type:'numeric', precision:0 },
			{ name: 'kd_barang', type: 'lov', size: 'short',
				onClearButton:function(){
					$(this).setval(''); 
					$('#nama_barang').setval(''); 
					$('#<?php echo $ls_gridname; ?>_nama_barang').setval('');
				},
				onActionButton:function(){ 
					LOVBarang({ onSelect:function(row){ 
						$('#<?php echo $ls_gridname; ?>_kd_barang').setval(row.kd_barang); 
						$('#<?php echo $ls_gridname; ?>_nama_barang').setval(row.nama_barang);
						$('#<?php echo $ls_gridname; ?>_harga_beli').setval(row.harga_jual); 
						$('#<?php echo $ls_gridname; ?>_margin').focus(); 
					} }); 
				}
			},
			{ name: 'nama_barang', type: 'text', size: '24', readonly:true },
			//{ name: 'harga_jual', type: 'text', size: '24', readonly:true },
			{ name: 'harga_jual', type: 'text', precision:2, readonly:true },
			{ name: 'harga_beli', type: 'text', size: '24' },
			{ name: 'jumlah', type: 'text', size: '24' },
			{ name: 'margin', type: 'text', size: '10' },
			{ name: 'sub_total', type: 'text', precision:2, readonly:true }
		], {}, { prefix:'<?php echo $ls_gridname; ?>_' });
		
		// Set Hidden Object
		$('#<?php echo $ls_gridname; ?>_kode_penjualan_detail').hide();
		
		// Create Dialog
		$('#form_<?php echo $ls_gridname; ?>_container').dialog({
			closed:true,
			modal:true,
			title: '_',
			width:550,
			height: 300,
			buttons:[{
				text:'Save',
				iconCls:'icon-save',
				handler:function(){
					var ls_mode			= $('#form_<?php echo $ls_gridname; ?>').data('mode');
					var row				= formToJSON('form_<?php echo $ls_gridname; ?>');
					var ls_error		= '';
					
					// Validasi
					
					if(row.kd_barang == ''){
						ls_error	+= 'Isi Barang\n';
					}
					if(row.jumlah == 0){
						ls_error	+= 'Isi Jumlah Barang\n';
					}
					
					// Eksekusi
					if(ls_error!=''){
						$.messager.alert('Error', nl2br(ls_error), 'error');
					}else{
						if(ls_mode=='create'){
							$('#<?php echo $ls_gridname; ?>').datagrid('appendRow', row );
							if($('#total_bayar').val().length == 0){
								$('#total_bayar').setval(row.sub_total);
								$('#harga_jual').setval(row.harga_jual);
								$('#harga_beli').setval(row.harga_beli);
								$('#margin').setval(row.margin);
							} else {
								var harga_sebelum = parseInt($('#harga_beli').val());
								var total_sebelum = parseInt($('#total_bayar').val());
								var beli_sebelum = parseInt($('#harga_beli').val());
								var margin1 = parseInt($('#margin').val());
								
								var total_baru = total_sebelum + parseInt(row.sub_total);
								var harga_baru = harga_sebelum + parseInt(row.harga_jual);
								var beli_baru = beli_sebelum + parseInt(row.harga_beli);
								var margin2 = $('#margin').val() + parseInt(row.margin);
								
								$('#harga_jual').setval(harga_jual);
								$('#total_bayar').setval(total_baru);
								$('#harga_beli').setval(row.harga_jual);
								$('#margin').setval(margin2);
							}
							$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
						} else {
							var selectedIndex = $('#<?php echo $ls_gridname; ?>').data('selectedIndex');
							
							updateRowFlag('<?php echo $ls_gridname; ?>', row); // Patching Flag Update
							$('#<?php echo $ls_gridname; ?>').datagrid('updateRow',{index:selectedIndex,row:row});
							
							$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
							jsonToForm(oSel, {prefix:'<?php echo $ls_gridname; ?>_'});
							$('#<?php echo $ls_gridname; ?>_kd_barang').val( $('#<?php echo $ls_gridname; ?>_kd_barang').val() );
							$('#<?php echo $ls_gridname; ?>_sub_total').val( $('#<?php echo $ls_gridname; ?>_sub_total').val() );
						}
					}
				}
			},{
				text:'Close',
				iconCls:'icon-cancel',
				handler:function(){
					$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
				}
			}]
		});

		$('#<?php echo $ls_gridname; ?>_margin').keyup(function() {
			var harga_beli = parseInt($('#<?php echo $ls_gridname; ?>_harga_beli').val());
			var margin = parseInt($('#<?php echo $ls_gridname; ?>_margin').val());
			var harga_jual = harga_beli + (harga_beli * (margin / 100));
			$('#<?php echo $ls_gridname; ?>_harga_jual').setval(harga_jual);
			
			if($('#<?php echo $ls_gridname; ?>_margin').val().length == 0){
				$('#<?php echo $ls_gridname; ?>_harga_jual').setval('');
			}
			
		});
		
		$('#<?php echo $ls_gridname; ?>_jumlah').keyup(function() {
			
			var harga_beli = parseInt($('#<?php echo $ls_gridname; ?>_harga_beli').val());
			var margin = parseInt($('#<?php echo $ls_gridname; ?>_margin').val());
			var harga_jual = harga_beli + (harga_beli * (margin / 100));
			
			var jumlah = parseInt($('#<?php echo $ls_gridname; ?>_jumlah').val());
			var sub_total = jumlah * harga_jual;
  			$('#<?php echo $ls_gridname; ?>_sub_total').setval(sub_total);

			if($('#<?php echo $ls_gridname; ?>_harga_jual').val().length == 0){
				$('#<?php echo $ls_gridname; ?>_sub_total').setval('');
			}
			
		});
		
	});
</script>
<div id="form_<?php echo $ls_gridname; ?>_container" style="padding:5px;width:600px;height:400px;">
	<div id="form_<?php echo $ls_gridname; ?>">
    <input id="kode_penjualan_detail" />
    <table class="tb_form" style="width:100%;" cellpadding="0" cellspacing="0">
    <tr>
        <td class="label">Barang</td>
        <td><input id="kd_barang" /><input id="nama_barang" /></td>
    </tr>
	<tr>
        <td class="label">Harga Beli</td>
        <td><input id="harga_beli" /></td>
    </tr>
    <tr>
        <td class="label">Margin</td>
        <td><input id="margin" /> (%)</td>
    </tr>
	<tr>
        <td class="label">Harga Jual</td>
        <td><input id="harga_jual" /></td>
    </tr>
	<tr>
        <td class="label">Jumlah</td>
        <td><input id="jumlah" /></td>
    </tr>
    <tr>
        <td class="label">Subtotal</td>
        <td><input id="sub_total" /></td>
    </tr>
    </table>
	</div>
</div>
<?php 

// ----------------- >> Table Detail << ----------------- ?>
<script type="text/javascript">
	$(document).ready(function(){
		$('#<?php echo $ls_gridname; ?>').datagrid({
			url:gs_path+'/'+controller+'/<?php echo $ls_gridname; ?>_data',
			queryParams:{ kode_penjualan:$('#kode_penjualan').getval(), rows:9999999999 },
			<?php if($mode!='view'){ 
			// -->> Toolbar [Start]
			// Only Appear On Mode Create / Edit ?>
			toolbar:[{
				text:'Tambah',
				iconCls:'icon-add',
				handler:function(){
					resetError();
					
					$('#form_<?php echo $ls_gridname; ?>_container').dialog('open');
					$('#form_<?php echo $ls_gridname; ?>').data('mode', 'create');
					$('#form_<?php echo $ls_gridname; ?>_container').prev().find('.panel-title').html('Tambah Detail Barang');					
					$('#form_<?php echo $ls_gridname; ?>').resetForm();
				}
			},'-',{
				text:'Ubah',
				iconCls:'icon-edit',
				handler:function(){
					var oSel=$('#<?php echo $ls_gridname; ?>').datagrid('getSelected'); 
					if(oSel==null){ $.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); }
					else{ 
						resetError();
						
						$('#form_<?php echo $ls_gridname; ?>_container').dialog('open');
						$('#form_<?php echo $ls_gridname; ?>').data('mode', 'edit');
						$('#form_<?php echo $ls_gridname; ?>_container').prev().find('.panel-title').html('Ubah Detail Barang');
						
						jsonToForm(oSel, {prefix:'<?php echo $ls_gridname; ?>_'});
					}
				}
			},'-',{
				text:'Hapus',
				iconCls:'icon-remove',
				handler:function(){
					var row = $('#<?php echo $ls_gridname; ?>').datagrid('getSelected');
					if(row==null){ $.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); }
					else if(row){
						if(!confirm('Hapus baris data?')) return false;

						var index = $('#<?php echo $ls_gridname; ?>').datagrid('getRowIndex', row);
						$('#<?php echo $ls_gridname; ?>').datagrid('deleteRow', index);
					}
				}
			}],<?php 
			} // -->> Toolbar [End]
			?>
			onClickRow:function(rowIndex){
				var lastIndex = $('#<?php echo $ls_gridname; ?>').data('selectedIndex');
				$('#<?php echo $ls_gridname; ?>').data('selectedIndex', rowIndex);
			},
			onLoadSuccess:function(){ }
		});
	});
	
</script>
<table id="<?php echo $ls_gridname; ?>" style="width:600;height:auto" singleSelect="true" idField="kode_penjualan_detail" showFooter="true">
<thead>
    <tr>
    	<th field="kd_barang" width="200">Kode Barang</th>
        <th field="nama_barang" width="200">Nama Barang</th>
        <th field="jumlah" width="120" align="right" formatter="formatNumericPoint2">Jumlah</th>
        <th field="margin" width="120" align="right" formatter="formatNumericPoint2">Margin (%)</th>
        <th field="sub_total" width="120" align="right" formatter="formatNumericPoint2">Sub Total</th>
    </tr>
</thead>
</table>
